﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System.IO;
using System.Collections;

namespace 根据TIN生成等高线
{
    public partial class Form1 : Form
    {
        AcadApplication AcadApp;
        AcadDocument AcadDoc;

        private ArrayList arrayContourPoints = new ArrayList();

        public Form1()
        {
            InitializeComponent();
        }

        private void 启动CAD()
        {
            try
            {
                AcadApp =
               (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
                AcadDoc = AcadApp.ActiveDocument;
            }
            catch
            {
                OpenFileDialog pOpenDG = new OpenFileDialog();
                pOpenDG.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf";
                pOpenDG.Title = "打开CAD文件";
                pOpenDG.ShowDialog();
                string filePath = pOpenDG.FileName;
                if (filePath == "")
                {
                    MessageBox.Show("选择CAD文件无效！", "文件无效！");
                    Application.Exit();
                }
                AcadApp = new AcadApplication();
                AcadDoc = AcadApp.Documents.Open(filePath, null, null);
            }
            AcadApp.Application.Visible = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            启动CAD();
        }

        private void 选择三角形面片_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet01");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "3DFACE";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            MessageBox.Show("共选择对象个数为：" + mySelectionSet.Count.ToString());
            AcadDoc.SelectionSets.Item("NewSelectionSet01").Delete();
        }

        private void CalContourPoint(double x1, double y1, double z1, double x2, double y2, double
z2, double intervalZ, int thisFaceIndex)
        {
            int minIndex, maxIndex, tempIndex;
            minIndex = (int)(z1 / intervalZ);
            maxIndex = (int)(z2 / intervalZ);

            if (minIndex == maxIndex)
            {
                return;
            }
            if (minIndex > maxIndex)
            {
                tempIndex = minIndex;
                minIndex = maxIndex;
                maxIndex = tempIndex;
            }
            double px, py, pz, tempValue;
            bool isMainContour = false;
            ContourPoint newContourPoint;
            for (int i = minIndex + 1; i < maxIndex + 1; i++)
            {
                pz = i * intervalZ;
                tempValue = (pz - z2) / (z1 - z2);
                px = x2 - tempValue * (x2 - x1);
                py = y2 - tempValue * (y2 - y1);
                if (i % 5 == 0)
                {
                    isMainContour = true;
                }
                newContourPoint = new ContourPoint();
                newContourPoint.x = px;
                newContourPoint.y = py;
                newContourPoint.z = pz;
                newContourPoint.faceIndex = thisFaceIndex;
                newContourPoint.mainContour = isMainContour;
                arrayContourPoints.Add(newContourPoint);
            }
        }

        private void 绘制等高线_Click(object sender, EventArgs e)
        {
            arrayContourPoints.Clear();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet02");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "3DFACE";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            double intervalHeight = Double.Parse(InterValZtextBox.Text);
            int acitveFaceIndex = -1;
            foreach (Acad3DFace returnFace in mySelectionSet)
            {
                acitveFaceIndex++;
                double[] triVertexCoords = new double[9];
                triVertexCoords = (double[])returnFace.Coordinates;
                CalContourPoint(triVertexCoords[0], triVertexCoords[1], triVertexCoords[2],
                triVertexCoords[3], triVertexCoords[4], triVertexCoords[5],
               intervalHeight, acitveFaceIndex);
                CalContourPoint(triVertexCoords[3], triVertexCoords[4], triVertexCoords[5],
                triVertexCoords[6], triVertexCoords[7], triVertexCoords[8],
               intervalHeight, acitveFaceIndex);
                CalContourPoint(triVertexCoords[0], triVertexCoords[1], triVertexCoords[2],
                triVertexCoords[6], triVertexCoords[7], triVertexCoords[8],
               intervalHeight, acitveFaceIndex);
            }
            AcadDoc.SelectionSets.Item("NewSelectionSet02").Delete();
            ContourPoint[] myContourPoint = new ContourPoint[arrayContourPoints.Count];
            arrayContourPoints.CopyTo(myContourPoint);
            //绘制等高线，根据高程值相同和所在面的序号相同这两个条件
            for (int i = 0; i < myContourPoint.Length; i++)
            {
                for (int j = 0; j < myContourPoint.Length; j++)
                {
                    if (i != j)
                    {
                        if (myContourPoint[i].z == myContourPoint[j].z &&
                       myContourPoint[i].faceIndex == myContourPoint[j].faceIndex)
                        {
                            double[] p1 = new double[3];
                            p1[0] = myContourPoint[i].x;
                            p1[1] = myContourPoint[i].y;
                            p1[2] = myContourPoint[i].z;
                            double[] p2 = new double[3];
                            p2[0] = myContourPoint[j].x;
                            p2[1] = myContourPoint[j].y;
                            p2[2] = myContourPoint[j].z;
                            AcadDoc.ModelSpace.AddLine(p1, p2);
                        }
                    }
                }
            }
            MessageBox.Show("绘制等高线完成！");
        }
    }
}
